﻿
package com.ostrich.manpower.controller;

import java.io.FileOutputStream;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.servlet.ModelAndView;

import com.base.util.HtmlUtil;
import com.base.web.BaseAction;
import com.ostrich.common.OstrichConstants;
import com.ostrich.manpower.bean.TostHrBaseinfo;
import com.ostrich.manpower.bean.TostHrBirgiftinfo;
import com.ostrich.manpower.bean.TostHrDepartinfo;
import com.ostrich.manpower.common.OstDateUtils;
import com.ostrich.manpower.service.impl.TostHrBaseinfoService;
import com.ostrich.manpower.service.impl.TostHrBirgiftinfoService;
import com.ostrich.manpower.service.impl.TostHrDepartinfoService;
import com.ostrich.system.analysis.ParaminfoUtil;
import com.ostrich.system.bean.TostSysParaminfo;

/**
 * ClassName: Controller <br/>
 * date: 2013-5-9 下午03:07:38 <br/>
 * 
 * @author wangyinan
 * @version
 */
@Controller
@RequestMapping("/tostHrBirgiftinfo")
public class TostHrBirgiftinfoController extends BaseAction
{
	private final static Logger log= Logger.getLogger(TostHrBirgiftinfoController.class);
	// Servrice start
	@Autowired(required=false) //自动注入，不需要生成set方法了，required=false表示没有实现类，也不会报错。
	private TostHrBirgiftinfoService<TostHrBirgiftinfo> tostHrBirgiftinfoService; 
	@Autowired(required=false) //自动注入，不需要生成set方法了，required=false表示没有实现类，也不会报错。
	private TostHrBaseinfoService<TostHrBaseinfo> tosthrbaseinfoservice; 
	@Autowired(required=false) //自动注入，不需要生成set方法了，required=false表示没有实现类，也不会报错。
	private TostHrDepartinfoService<TostHrBaseinfo> tosthrdepartinfoservice; 
	/**
	 * ilook 首页
	 * @param url
	 * @param classifyId
	 * @return
	 */
	@RequestMapping("/list") 
	public ModelAndView  list(TostHrBirgiftinfo tostHrBirgiftinfo,HttpServletRequest request) throws Exception{
		Map<String,Object>  context = getRootMap();
		List<TostHrBirgiftinfo> dataList = tostHrBirgiftinfoService.queryByList(tostHrBirgiftinfo);
		//设置页面数据
		context.put("dataList", dataList);
		return forword("manpower/tabTostHrBirgiftinfo",context); 
	}
	
	//查询公司
	@RequestMapping("/queryByhrCompany")
	public void queryByhrCompany(HttpServletResponse response){
		//获得id
		Map<Integer, TostSysParaminfo> cmps = ParaminfoUtil.getParamByPid(OstrichConstants.PARMHR_BASE_COMPANY);
		List<TostSysParaminfo> ls = new ArrayList<TostSysParaminfo>();
		for(Map.Entry<Integer, TostSysParaminfo> cp:cmps.entrySet()){
			ls.add(cp.getValue());
		}
		HtmlUtil.writerJson(response, ls);
	}
	
	//查询人员类别
		@RequestMapping("/queryByhrhrGrades")
		public void queryByhrhrGrades(HttpServletResponse response){
			Map<Integer, TostSysParaminfo> cmps = ParaminfoUtil.getParamByPid(OstrichConstants.PARMHR_BASE_PERGRADE);
			List<TostSysParaminfo> ls = new ArrayList<TostSysParaminfo>();
			for(Map.Entry<Integer, TostSysParaminfo> cp:cmps.entrySet()){
				ls.add(cp.getValue());
			}
			HtmlUtil.writerJson(response, ls);
		}
		
		//查询部门名称
		@RequestMapping("/queryDepartId")
	         public void  queryDepartId(HttpServletResponse response){
			    List<TostHrDepartinfo> dataList =tosthrdepartinfoservice.queryDapart();
				HtmlUtil.writerJson(response, dataList);
		}
	
	/**
	 * json 列表页面
	 * @param url
	 * @param classifyId
	 * @return
	 * @throws Exception 
	 */
	@RequestMapping("/dataList") 
	public void  querylastbirlist(TostHrBirgiftinfo tostHrBirgiftinfo,HttpServletResponse response) throws Exception{	
		List<TostHrBirgiftinfo> dataList = tostHrBirgiftinfoService.queryByList(tostHrBirgiftinfo);
		/*for(TostHrBirgiftinfo ud: dataList){
			//这里可以做数据封装到对象中。
		}*/
		//设置页面数据
		Map<String,Object> jsonMap = new HashMap<String,Object>();
		jsonMap.put("total",tostHrBirgiftinfo.getPager().getRowCount());
		jsonMap.put("rows", dataList);
		HtmlUtil.writerJson(response, jsonMap);
	}
	/**
	 * 添加或修改数据
	 * @param url
	 * @param classifyId
	 * @return
	 * @throws Exception 
	 */
	@RequestMapping("/save")
	public void save(TostHrBirgiftinfo bean,HttpServletResponse response) throws Exception{
		bean.setHbrMonth(OstDateUtils.getDateByMonth(bean.getHbrMonth()));
		if (bean.getId() == null) {
			tostHrBirgiftinfoService.add(bean);
		} else {
			tostHrBirgiftinfoService.update(bean);
		}
		sendSuccessMessage(response, "保存成功~");
	}
	@RequestMapping("/getId")
	public void getId(Integer id,HttpServletResponse response) throws Exception{
		Map<String,Object>  context = getRootMap();
		TostHrBirgiftinfo bean  = tostHrBirgiftinfoService.queryById(id);
		if(bean  == null){
			sendFailureMessage(response, "没有找到对应的记录!");
			return;
		}
		context.put(SUCCESS, true);
		context.put("data", bean);
		HtmlUtil.writerJson(response, context);
	}
	
	@RequestMapping("/delete")
	public void delete(Integer[] id,HttpServletResponse response) throws Exception{
		tostHrBirgiftinfoService.delete(id);
		sendSuccessMessage(response, "删除成功");
	}
	@RequestMapping("/queryBymonthbir")
	public void queryBymonthbir(HttpServletResponse response) throws Exception{
		Map<String,Object>  context = getRootMap();
		List<TostHrBaseinfo> Bymonthbir= tosthrbaseinfoservice.queryBymonthbir();	
		context.put("total",(Bymonthbir!=null)?Bymonthbir.size():0);
		context.put("data", Bymonthbir);
		HtmlUtil.writerJson(response, context);
	}
	
	
	//写进生日礼金表
	@RequestMapping("/insectpgift")
	public void insectBirGift(HttpServletRequest request,String brhbrMonth,Integer noformal,Integer formal,String hrId,HttpServletResponse response) throws Exception{
		String str1=brhbrMonth.substring(0, 4);
		String str2=brhbrMonth.substring(5, 7);
		String str=str1+str2;
		try{
			if(this.isHavalastmm(response)){
			tostHrBirgiftinfoService.setBirgiftByHrids(str,hrId,noformal,formal);
			sendSuccessMessage(response, "设置成功！");
			}
			else {
				sendSuccessMessage(response, "设置失败！原因"+str2+"月已经设置");					
			}
		}catch (Exception e){
			sendSuccessMessage(response, "设置失败！");
		}
	}
	
	//设置发放月份
	public boolean isHavalastmm(HttpServletResponse response){
		boolean islenth;
		Integer interger=tostHrBirgiftinfoService.isHavalastmm();
		if(interger!=0){
            islenth=false;
		}
		else {
			islenth=true;
		}
		return islenth;
	}
	@RequestMapping("/delLastBirgift")
	public ModelAndView delLastBirgift(HttpServletResponse response,TostHrBirgiftinfo  bean) throws Exception{		
		tostHrBirgiftinfoService.delLastBirgift();
		return new ModelAndView("forward:/tostHrBirgiftinfo/list.do");
	}

	
	@RequestMapping(value = "/upload")
	public ModelAndView upload(HttpServletRequest request,
			HttpServletResponse response, ModelMap model) throws Exception {
		MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
		// 得到上传的文件
		MultipartFile mFile = multipartRequest.getFile("excelPath");
		// 得到上传服务器的路径
		String path = request.getSession().getServletContext()
				.getRealPath("/WEB-INF/upload/");
		// 得到上传的文件的文件名
		String filename = mFile.getOriginalFilename();
		InputStream inputStream = mFile.getInputStream();
		byte[] b = new byte[1048576];
		int length = inputStream.read(b);
		path += "\\" + filename;
		// 文件流写到服务器端
		FileOutputStream outputStream = new FileOutputStream(path);
		outputStream.write(b, 0, length);
		inputStream.close();
		outputStream.close();
		request.setAttribute("filepath", path);
		//request.setAttribute("month", request.getParameter("hsbMonth"));
		return new ModelAndView("forward:/tostHrBirgiftinfo/upimport.do");
	}
	
	@RequestMapping(value = "/upimport")
	public ModelAndView upimport(HttpServletRequest request,
			HttpServletResponse response) {
		Map<String, Object> context = getRootMap();
		String path = (String)request.getAttribute("filepath");
		try {
			String msg = tostHrBirgiftinfoService.setBirgiftByFilepath(path);
			msg=(msg!=null && msg.trim().length()>0)?"您已经成功导入。但是还有："+msg:"您已经导入成功请查看。";
			request.setAttribute("msg",msg );
			request.setAttribute("success", "您已经导入成功请查看!");
		}catch (Exception e) {
			e.printStackTrace();
			request.setAttribute("msg", "导入失败，请查看xls文件是否模板正确");
		}
		request.setAttribute("url", request.getContextPath()+"/tostHrBirgiftinfo/list.do");
		return forword("penmsg2",context);
	}

}
